package com.zyk.leetcode;

import java.util.Stack;

/**
 * @author zhangsan
 * @date 2021/4/9 11:33
 */
public class M0305 {

    // https://leetcode-cn.com/problems/sort-of-stacks-lcci/
    static class SortedStack {

        // 栈顶为最小元素, 且有序的栈.
        Stack<Integer> elements, helpPush;

        public SortedStack() {
            elements = new Stack<>();
            helpPush = new Stack<>();
        }

        public void push(int val) {
            while (!elements.isEmpty() && val > elements.peek() ) {
                helpPush.push(elements.pop());
            }
            elements.push(val);
            while (!helpPush.isEmpty()) {
                elements.push(helpPush.pop());
            }
        }

        public void pop() {
            if(!elements.isEmpty()) {
                elements.pop();
            }
        }

        public int peek() {
            return elements.peek();
        }

        public boolean isEmpty() {
            return elements.isEmpty();
        }
    }


    // for test
    public static void main(String[] args) {
        // ["SortedStack", "push", "push", "peek", "pop", "peek"]
        // [[], [1], [2], [], [], []]

        /*SortedStack stack = new SortedStack();
        stack.push(1);
        stack.push(2);
        System.out.println(stack.peek());
        stack.pop();
        System.out.println(stack.peek());*/

        // ["SortedStack", "pop", "pop", "push", "pop", "isEmpty"]
        // [[], [], [], [1], [], []]

        SortedStack stack = new SortedStack();
        stack.pop();
        stack.pop();
        stack.push(1);
        stack.pop();
        System.out.println(stack.isEmpty());
    }

}
